﻿using System;

namespace Game
{
    class Game
    {
        Font sans56;
            
        bool gameFinished;
        Player[] lemming;
        int numLemmings;
        int maxLemming;
        int count;
        Image background;
        Image options;
        float percentage; //Porcentaje completado
        Image arrow;
        int selectedAbility;

        public void Run()
        {
            count = 0;
            percentage = 0.00f; //Variable para la condicion de victoria
            maxLemming = 15; 
            gameFinished = false;
            lemming = new Player[maxLemming];
            numLemmings = 0;
            background = new Image("data/BackgroundLevel1.png");
            options = new Image("data/options.png");
            sans56 = new Font("data/Joystix.ttf", 56);
            selectedAbility = 0; ;


            arrow = new Image("data/flecha.png");
            //lemming[numLemmings-1] = new Player();//un player
            
            while (!gameFinished)
            {
                DrawElements();
                CheckInputDevices();
                MoveElements();
                CheckCollisions();
                PauseTillNextFrame();
                ControlConsole();
            } // end of game loop
        }

        public void DrawElements()
        {
            Hardware.ClearScreen();
            Hardware.DrawHiddenImage(background, 0, 0);
            Hardware.DrawHiddenImage(options, 0, 528);
            
            Hardware.WriteHiddenText(DateTime.Now.Second.ToString(),
                    505, 475,
                    0x00, 0xB0, 0x00,
                    sans56);

            Hardware.WriteHiddenText(numLemmings.ToString("00"),
               150, 473,//196
               0x00, 0xB0, 0x00,
               sans56);
            
            Hardware.WriteHiddenText(percentage.ToString("00"),
             290, 473,
             0x00, 0xB0, 0x00,
             sans56);

            if (count == 0 && numLemmings < maxLemming)
            {
                lemming[numLemmings] = new Player();
                numLemmings++;
                percentage += 6.66f;
            }

            count++;

            if (count == 100)
                count = 0;
            
            for (int i = 0; i < numLemmings; i++)
                lemming[i].DrawOnHiddenScreen();

            Hardware.ShowHiddenScreen();
        }


        public void CheckInputDevices()
        {
           /* if (Hardware.KeyPressed(Hardware.KEY_RIGHT))
                lemming[numLemmings].MoveRight();
            if (Hardware.KeyPressed(Hardware.KEY_LEFT))
                lemming[numLemmings].MoveLeft();
            if (Hardware.KeyPressed(Hardware.KEY_UP))
                lemming[numLemmings].MoveUp();
            if (Hardware.KeyPressed(Hardware.KEY_DOWN))
                lemming[numLemmings].MoveDown();*/

            

            if (Hardware.KeyPressed(Hardware.KEY_ESC))
                gameFinished = true;

            int posXMouse = 0;
            int posYMouse = 0;

            if (Hardware.MouseKeyDown(out posXMouse, out posYMouse))
            {
                if (posXMouse > 360 &&
                   posXMouse < 400 &&
                   posYMouse > 520 &&
                   posYMouse < 600)
                {
                    selectedAbility = 1;
                }

                for (int i = 0; i < numLemmings; i++)
                {
                    if (posYMouse >= lemming[i].GetPositionY() &&
                        posYMouse <= lemming[i].GetPositionY() + 18 &&
                        posXMouse >= lemming[i].GetPositionX() &&
                        posXMouse <= lemming[i].GetPositionX() + 10)
                    {
                        lemming[i].DoAbility(selectedAbility);
                    }
                }
                
            
            }
        }


        public void MoveElements()
        {
            //ghost.Move();
        }


        public void CheckCollisions()
        {
            for (int i = 0; i < numLemmings; i++)
            {
                if (lemming[i].GetPositionY() == 203)
                {
                    lemming[i].SetMovingDown(false);

                    if (lemming[i].GetPositionX() == 518)
                        lemming[i].SetMovingRight(false);

                    if (lemming[i].GetPositionX() == 114)
                        lemming[i].SetMovingRight(true);
                        
                                
                    if (lemming[i].GetMovingRight())
                        lemming[i].MoveRight();
                    else        
                        lemming[i].MoveLeft();
                }
                else
                    lemming[i].SetMovingDown(true);
                            
                if (lemming[i].GetMovingDown())
                    lemming[i].MoveDown();
            }
        }


        public static void PauseTillNextFrame()
        {
            Hardware.Pause(40);
        }

        public void ControlConsole()
        {
            Console.Clear();
            for (int i = 0; i < numLemmings; i++)
                Console.WriteLine((i+1).ToString("00") + " " + lemming[i].GetPositionX() + " x ! " + lemming[i].GetPositionY() + " y");
            Console.WriteLine(count);
        }
    }
}
